﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace exSplitter
{
    public class Constants
    {
        public const string Permssion = "2";
    }
    /// <summary>
    /// BindingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BindingWindow : Window
    {
        public static Person p = new Person();
        private bool isSwich;

        public BindingWindow()
        {
            InitializeComponent();

            p.Name = "1";
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            isSwich = !isSwich;
            p.Name = isSwich ? "2" : "1";
        }
    }

    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                //触发事件
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
    }

    public class StrConverter : IValueConverter
    {
        public static readonly StrConverter Current = new StrConverter();
        /// <inheritdoc/>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var str = value as string;
            var parm = parameter as string;
            return str == parm ? Visibility.Visible : Visibility.Collapsed;
        }


        /// <inheritdoc/>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

    public class AttachProperty
    {
        /// <summary>
        /// 获取ResourceBindingKeyHelper附加属性
        /// </summary>
        /// <param name="obj">DependencyObject</param>
        /// <returns>object</returns>
        public static object GetResourceBindingKeyHelper(DependencyObject obj)
        {
            return (object)obj.GetValue(ResourceBindingKeyHelperProperty);
        }

        /// <summary>
        /// 设置ResourceBindingKeyHelper附加属性
        /// </summary>
        /// <param name="obj">DependencyObject</param>
        /// <param name="value">附加属性值</param>
        public static void SetResourceBindingKeyHelper(DependencyObject obj, object value)
        {
            obj.SetValue(ResourceBindingKeyHelperProperty, value);
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for ResourceBindingKeyHelper.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty ResourceBindingKeyHelperProperty =
            DependencyProperty.RegisterAttached("ResourceBindingKeyHelper", typeof(object), typeof(AttachProperty),
                new PropertyMetadata(null, ResourceKeyChanged));

        static void ResourceKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = d as FrameworkElement;
            var newVal = e.NewValue as string;
            Binding binding = new Binding();
            binding.Source = BindingWindow.p;
            binding.Path = new PropertyPath("Name");
            binding.Converter = StrConverter.Current;
            binding.ConverterParameter = "2";
            target.SetBinding(FrameworkElement.VisibilityProperty, binding);
        }
    }
}
